define([
    'zepto',
    'underscore',
    'backbone',
    "text!templates/home.html",
    "text!templates/myCard.html",
    "text!templates/cardList.html",
    "text!templates/cardList2.html",
    "text!templates/agreement.html",
    "wTools"

], function ($, _, Backbone, template, myCard, cardList, cardList2, agreement) {
    return Backbone.View.extend({
        $el: $main,
        el: $main0,
        template: _.template(template),
        templateMyCard: _.template(myCard),
        templateCardList: _.template(cardList),
        templateCardList2: _.template(cardList2),
        templateAgreement: _.template(agreement),
        events: {
            "tap #card": "Card",
            "tap .chooseCardBg": "closeList",
            "tap .cardList li.chooseCard1": "chooseCard",
            "tap .cardList2 li.chooseCard1": "chooseCard2",
            "tap #cardListOut": "newCard",
            "tap #showAgreement": "showAgreement",
            "tap #closeAgreement": "closeAgreement",
            "tap #check_Icon": "changeCheck",

            "tap #getVerify": "getVerify",
            "tap #Pay.btn": "pay"
        },
        canGet: true,
        Interval: -1,
        initialize: function () {

            $("#head_back").hide();

            if (wData.urlData.token != undefined && wData.urlData.token != "" && wData.urlData.token != null && wData.urlData.token != wData.token) {
                wData.token = wData.urlData.token;
            }
            if (wData.token != undefined && wData.token != "" && wData.token != null) {
                $.cookie("token", wData.token, {path: "/"});
            } else if ($.cookie("token") != undefined && $.cookie("token") != "" && $.cookie("token") != null) {
                wData.token = $.cookie("token");
            } else {
                wData.errorMessage = "操作超时,请重试";
                location.hash = "fail";
            }

            if (wData.orderInfo == undefined || wData.orderInfo == '' || wData.orderInfo == null) {

                var postData = {
                    token: wData.token
                }, that = this;
                wAjaxForm("getOrderInfo", "", postData, function (result) {
                    if (result.success == true) {
                        wData.noCard = false;
                        wData.cardList = result.data.qpayCardInfos;
                        wData.orderInfo = result.data.tradeVoucherList[0];
                        wData.orderInfoTouchuan={
                            payerName:"",
                            idNumber:"",
                            phoneNumber:"",
                            payerCardNo:"",
                            cardType:""
                        };
                        if(result.data.buyerInfo!=undefined&&result.data.buyerInfo!=null) {
                            wData.orderInfoTouchuan = result.data.buyerInfo;
                        }
                        if (!($("#order").length > 0)) {
                            $("#home").append(that.template(wData.orderInfo));
                        }

                        //透传处理
                        if (wData.orderInfoTouchuan.payerCardNo !=undefined&&wData.orderInfoTouchuan.payerCardNo !=null&&wData.orderInfoTouchuan.payerCardNo !="") {
                            //透传卡号已绑定
                            if (wData.cardList.length > 0) {
                                wData.selectCardIndex="0";
                                wData.selectCard = wData.cardList[wData.selectCardIndex];
                                that.$el.append(that.templateMyCard(wData.selectCard));
                            }
                            //透传卡号未绑定
                            else{

                                var postData = {
                                    cardNo: wData.orderInfoTouchuan.payerCardNo,
                                    partnerId: wData.orderInfo.partnerId,
                                    productCode: wData.orderInfo.productCode,
                                    accessChannel: "WAP",
                                    dbcr: wData.orderInfoTouchuan.cardType
                                }, thats = that;
                                wAjaxForm("checkBankCard", "", postData, function (result) {
                                    if (result.success == true) {
                                        if (result.data.returnCode == "101") {
                                            wData.cardNo = wData.orderInfoTouchuan.payerCardNo;
                                            wData.otherCard = result.data.payChannelList[0];
                                            wData.selectCardIndex = "-1";
                                            wData.cardType = wData.otherCard.dbcr;
                                            //that.$el.append(that.templateMyCard(wData));
                                            thats.more();
                                            thats.$el.append(thats.templateMyCard(wData));

                                            if (wData.orderInfoTouchuan.payerName != undefined && wData.orderInfoTouchuan.payerName != null && wData.orderInfoTouchuan.payerName != "") {
                                                $("#idName").val(wData.orderInfoTouchuan.payerName).attr("readonly",true);
                                            }
                                            if (wData.orderInfoTouchuan.idNumber != undefined && wData.orderInfoTouchuan.idNumber != null && wData.orderInfoTouchuan.idNumber != "") {
                                                $("#idCard").val(wData.orderInfoTouchuan.idNumber).attr("readonly",true);
                                            }
                                            if (wData.orderInfoTouchuan.phoneNumber != undefined && wData.orderInfoTouchuan.phoneNumber != null && wData.orderInfoTouchuan.phoneNumber != "") {
                                                $("#mobileNo").val(wData.orderInfoTouchuan.phoneNumber).attr("readonly",true);
                                            }

                                        } else {
                                            wTips("银行卡号格式不正确");
                                        }
                                    }
                                    else {
                                        wTips(result.message);
                                    }
                                });
                            }
                        }
                        else {

                            if (wData.cardList.length > 0) {
                                that.$el.append(that.templateCardList2(wData));
                            }
                            else {
                                wData.noCard = true;
                                location.hash = "newCard";
                            }
                        }
                    }
                    else {

                        wData.errorMessage=result.message;
                        location.hash = "fail";
                    }
                });
            } else {
                if (!($("#order").length > 0)) {
                    $("#home").append(this.template(wData.orderInfo));
                }

                if (wData.otherCard == undefined && wData.selectCardIndex == undefined) {
                    this.$el.append(this.templateCardList2(wData));
                } else {
                    this.$el.append(this.templateMyCard(wData));

                    //透传处理
                    if (wData.selectCardIndex == "-1") {


                        if (wData.orderInfoTouchuan.payerName != undefined && wData.orderInfoTouchuan.payerName != null && wData.orderInfoTouchuan.payerName != "") {
                            $("#idName").val(wData.orderInfoTouchuan.payerName).attr("readonly",true);
                        }
                        if (wData.orderInfoTouchuan.idNumber != undefined && wData.orderInfoTouchuan.idNumber != null && wData.orderInfoTouchuan.idNumber != "") {
                            $("#idCard").val(wData.orderInfoTouchuan.idNumber).attr("readonly",true);
                        }
                        if (wData.orderInfoTouchuan.phoneNumber != undefined && wData.orderInfoTouchuan.phoneNumber != null && wData.orderInfoTouchuan.phoneNumber != "") {
                            $("#mobileNo").val(wData.orderInfoTouchuan.phoneNumber).attr("readonly",true);
                        }
                    }
                }


            }
        },
        //获取验证码
        getVerify: function () {
            if ($("#getVerify").hasClass("color_63b4ff")) {
                this.canGet = true;

                var postData;
                if (wData.selectCardIndex == "-1") {
                    if (wData.cardType == "CC") {
                        //未绑卡发送验证码
                        if (wCheckBool($("#idName").val(), "^([\u4e00-\u9fa5]){2,7}$","姓名格式错误", "姓名") && wCheckBool($("#idCard").val(), "^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9]|X)$", "", "身份证号") && wCheckBool($("#cvn").val(), "^\\d{3}$", "", "CVN码") && wCheckBool($("#date").val(), "^(0[1-9])|(1[0-2])\\d{2}$", "", "有效期") && wCheckBool($("#mobileNo").val(), "^0?(13|15|18|14|17|16|19)[0-9]{9}$", "", "手机号码")) {

                            postData = {
                                instId: wData.otherCard.instId,
                                token: wData.token,
                                companyOrPersonal: wData.otherCard.companyOrPersonal,
                                bankcardnum: wData.cardNo,
                                bankCode: wData.otherCard.instCode,
                                bankName: encodeURI(wData.otherCard.instName),
                                idName: encodeURI($("#idName").val()),
                                idCard: $("#idCard").val(),
                                month: $("#date").val().substring(0, 2),
                                year: $("#date").val().substring(2, 4),
                                cvNum: $("#cvn").val(),
                                mobile: $("#mobileNo").val(),
                                dbcr: wData.otherCard.dbcr
                            };
                        } else {
                            this.canGet = false;
                        }
                    } else {
                        if (wCheckBool($("#idName").val(),  "^([\u4e00-\u9fa5]){2,7}$", "姓名格式错误", "姓名") && wCheckBool($("#idCard").val(), "^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9]|X)$", "", "身份证号") && wCheckBool($("#mobileNo").val(), "^0?(13|15|18|14|17|16|19)[0-9]{9}$", "", "手机号码")) {

                            postData = {
                                instId: wData.otherCard.instId,
                                token: wData.token,
                                companyOrPersonal: wData.otherCard.companyPersonal,
                                bankcardnum: wData.cardNo,
                                bankCode: wData.otherCard.instCode,
                                bankName: encodeURI(wData.otherCard.instName),
                                idName: encodeURI($("#idName").val()),
                                idCard: $("#idCard").val(),
                                month: "",
                                year: "",
                                cvNum: "",
                                mobile: $("#mobileNo").val(),
                                dbcr: wData.otherCard.dbcr
                            };
                        } else {
                            this.canGet = false;
                        }
                    }
                }
                else {
                    postData = {
                        token: wData.token,
                        bankcard: wData.selectCard.id
                    };
                }
                if (this.canGet == true) {
                    var that = this;
                    wAjaxForm("resendSms", "", postData, function (result) {
                        if (result.success == true) {
                            $("#Pay").removeClass("btn_gray").addClass("btn");
                            wData.payVoucherNo = result.data.payVoucherNo;
                            $("#getVerify").removeClass("color_63b4ff").addClass("color_9C9B9B");
                            var timeSix = 60;
                            $("#getVerify").html("已发送(" + timeSix + "s)");
                            var IntervalNo = setInterval(function () {
                                timeSix--;
                                if (timeSix == 0) {
                                    clearInterval(IntervalNo);
                                    that.Interval = -1;
                                    $("#getVerify").html("免费获取").removeClass("color_9C9B9B").addClass("color_63b4ff");
                                } else {
                                    $("#getVerify").html("已发送(" + timeSix + "s)");
                                }
                            }, 1000);
                            that.Interval = IntervalNo;
                        }
                        else {
                            if (result.message != "" && result.message != undefined && result.message != null) {
                                wTips(result.message);
                            } else {
                                wTips("系统繁忙");
                            }
                        }
                    });
                }
            }
        },
        //确认支付
        pay: function () {
            if (!$("#check_Icon").hasClass("check")) {
                wTips("请阅读并接受快捷支付服务协议");
            } else {

                if (wCheckBool($("#verifyNo").val(), "(^\\d{6}$)|(^\\d{4}$)", "", "验证码")) {

                    var postData = {
                        token: wData.token,
                        msg: $("#verifyNo").val(),
                        payVoucherNo: wData.payVoucherNo
                    }, that = this;
                    wAjaxForm("pay", "", postData, function (result) {
                        if (result.success == true) {
                            if (result.data.tranStatus == "S") {
                                that.more();
                                location.hash = "success";
                            } else if (result.data.tranStatus == "P") {
                                that.more();
                                location.hash = "wait";
                            }
                        }
                        else {

                            wTips(result.message);
                            //此处需要处理失败返回信息
                            //that.more();
                            //wData.errorMessage=result.message;
                            //location.hash = "fail";
                        }
                    });
                }
            }
        },

        //返回地址跳转
        returnUrl: function () {
            //location.href="http://www.baidu.com"//返回地址backURL;
        },
        //更改是否接受服务协议
        changeCheck: function () {
            if ($("#check_Icon").hasClass("check")) {
                $("#check_Icon").removeClass("check").addClass("notCheck");
            } else {
                $("#check_Icon").removeClass("notCheck").addClass("check");
            }
        },
        //显示协议内容
        showAgreement: function () {
            this.$el.append(this.templateAgreement());
            App.title("快捷支付服务协议");
        },
        //阅读并接受协议
        closeAgreement: function () {
            if (!$("#check_Icon").hasClass("check")) {
                $("#check_Icon").removeClass("notCheck").addClass("check");
            }
            $("#agreement").remove();
            App.title("订单支付");
        },
        //展开订单信息
        more: function () {
            if ($("#more").html() == "收起") {
                $("#order").css({"height": "2.6rem"});
                $("#more").html("更多");
            }
        },
        //打开其他银行卡列表
        Card: function () {

            //透传处理
            if (wData.orderInfoTouchuan.payerCardNo ==undefined||wData.orderInfoTouchuan.payerCardNo ==null||wData.orderInfoTouchuan.payerCardNo =="") {

                this.$el.append(this.templateCardList());
                $(".chooseCardBg").css({"height": $(window).height()});

                if (wData.cardList == undefined || wData.cardList == '' || wData.cardList == null) {

                }
            }

            //$("#cardList_" + wData.selectCardIndex).removeClass("noCheck").addClass("check");

        },
        //更改为其他银行卡
        chooseCard: function (e) {
            if (wData.selectCardIndex != e.currentTarget.id) {
                $("#cardList").remove();
                $("#order_content").remove();
                wData.selectCardIndex = e.currentTarget.id;
                wData.selectCard = wData.cardList[wData.selectCardIndex];
                this.$el.append(this.templateMyCard(wData.selectCard));
                if (this.Interval != -1) {
                    clearInterval(this.Interval);
                    this.Interval = -1;
                    $("#getVerify").html("免费获取").removeClass("color_9C9B9B").addClass("color_63b4ff");
                }
            } else {
                this.closeList();
            }
        },
        chooseCard2: function (e) {
            this.more();
            $("#cardList2").remove();
            wData.selectCardIndex = e.currentTarget.id;
            wData.selectCard = wData.cardList[wData.selectCardIndex];
            this.$el.append(this.templateMyCard(wData.selectCard));
        },
        //未做更改,关闭银行卡列表
        closeList: function () {
            $("#cardList").remove();
        },
        //选择使用其他未绑定银行卡支付
        newCard: function () {
            $("#cardList").remove();
            $("#order_content").remove();
            this.more();
            location.hash = "newCard";

        },
        back: function () {
            //history.back();
        },
        render: function (data) {
            var html = this.template(data);
            this.$el.html(html);
        },
        destroy: function () {
            this.undelegateEvents();
            this.$el.html("");
            $("#head_back").show();
            if (this.Interval != -1) {
                clearInterval(this.Interval);
                this.Interval = -1;
                $("#getVerify").html("免费获取").removeClass("color_9C9B9B").addClass("color_63b4ff");
            }
            delete this;
        }
    });
});